package com.hry.myapp.controllers;

import java.io.File;
import java.io.IOException;
import java.util.HashMap;
import java.util.Map;
import java.util.UUID;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestPart;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.multipart.MultipartFile;

import com.hry.myapp.beans.WXAppInfo;
import com.hry.myapp.beans.WXServiceInfo;

@Controller
@RequestMapping("/wxfilehandler")
public class WXFileHandleController {
	
	@Autowired
	WXAppInfo wxAppInfo ;
	
	@Autowired
	WXServiceInfo wxServiceInfo ;
	
	@ResponseBody
	@PostMapping("/avatar")
	public Map uploadAvatar(@RequestPart("avatar") MultipartFile avatar) throws IllegalStateException, IOException {
		
		
		UUID uuid = UUID.randomUUID(); 
		
		String originName = avatar.getOriginalFilename() ;
		
		
		String avatarName = uuid.toString() +"_"+ originName ;
		String dest = wxServiceInfo.getAvatarCompleteDestPath() + avatarName ;
		
		System.out.println(dest);
		
		avatar.transferTo(new File(dest));
		
		Map resultMap = new HashMap<>() ;
		
		String avatarUrl = wxServiceInfo.getAvatarCompleteUrlPath()+avatarName ;
		
		System.out.println("avatarUrl:"+avatarUrl);
		
		resultMap.put("avatarUrl", avatarUrl) ;
		
		return resultMap ;
	}
	
}
